%Script shows the EMG data of the selected suject and task.
% Requirements:
%   - 'subj_id': Number of the selected subjet
%   - 'task_id': Number of the selected task

clear;
close all
clc
addpath aux_functions

subj_id=4;  %Number of subject
task_id=6; %Number of task 
% each task was performed three times in a row
% the invalid data (due to e.g. measurement failure) were removed

% 'Task 0' 'Initinal & relaxed pose';
% 'Task 1' 'Ok gesture';
% 'Task 2' 'Thumb down';
% 'Task 3' 'Exultation';
% 'Task 4' 'Hitchhiking';
% 'Task 5' 'Block out sun from own face';
% 'Task 6' 'Greet';'Task 7' 'Military salute';
% 'Task 8' 'Stop gesture';'Task 9' 'Pointing';
% 'Task 10' 'Silence gesture';
% 'Task 11' 'Reach and grasp a small suitcase';
% 'Task 12' 'Reach and grasp a glass';
% 'Task 13' 'Reach and grasp a phone receiver';
% 'Task 14' 'Reach and grasp a book';
% 'Task 15' 'Reach and grasp a small cup';
% 'Task 16' 'Reach and grasp a an apple';
% 'Task 17' 'Reach and grasp a hat';
% 'Task 18' 'Reach and grasp a cup';
% 'Task 19' 'Receive a tray from someone';
% 'Task 20' 'Reach and grasp a key in a lock';
% 'Task 21' 'Reach and grasp a bottle';
% 'Task 22' 'Reach and grasp a smatphone';
% 'Task 23' 'Reach and grasp a tothbrush';
% 'Task 24' 'Reach and grasp a laptop';
% 'Task 25' 'Reach and grasp a pen';
% 'Task 26' 'Reach and grasp a pencil';
% 'Task 27' 'Reach and grasp a tea bag';
% 'Task 28' 'Reach and grasp a doorknob';
% 'Task 29' 'Reach and grasp a tennis ball';
% 'Task 30' 'Reach and grasp a cap of a bottle';
% 'Task 31' 'Vertical movement of eyes';
% 'Task 32' 'Horizontal movement of eyes';
% 'Task 33' 'Open/close eyes';

task_id=task_id+1;
load_path=strcat(pwd,'\subj_',sprintf('%03d',subj_id),'\trial_info.mat');
load(load_path);


trials_EMG=trial_info(task_id).EMG;
size_trials_EMG=size(trials_EMG);
for k=1:size_trials_EMG(1,2)
    i=trials_EMG(k);
    load_path=strcat(pwd,'\subj_',sprintf('%03d',subj_id),'\EMG\',strcat('trial_',sprintf('%03d',i)),'.mat');
    load(load_path)
    data_prepared_EMG = emg_uni2bi(refaraw);
    emgf=data_prepared_EMG;

    linewidth = 1;
    font_size = 8;
    l_font_size = 7;

    % figure position and size
    fheight= -0.25;
    fwidth= 0.000;
    mleft= -0.000;
    rleft= -0.0;
    mbottom = 0.18;
    dbottom= 0.26;
    name=char(strcat('Subj',{' '},sprintf('%03d',subj_id),{'   '},'Task',{' '},sprintf('%03d',task_id),{'   '},'Trial',{' '},sprintf('%03d',i)));
    figure('Name',name,'units','centimeters','position',[1+2*k 1+k 1.25*[27.94*0.921 21*0.921*0.921]]); % [left bottom width height]
    muscle_name = getMuscleNames;
    cntSubplots = 8;
    cntMuscles = 32;
    for J = 1:(cntMuscles/cntSubplots)
        for I = 1:cntSubplots
            K = I + (J-1)*cntSubplots;
            if K == 19 || K == 25 || K == 26
                subplot(cntSubplots,(cntMuscles/cntSubplots), (cntMuscles/cntSubplots)*(I-1) + J)
                plot(0,0);
                text(0,0,'This measurement channel was not used.', 'FontSize', font_size-0.5);
                xlim([-0.1, 7]);
                continue;
            end
            D = 0;
            if K > 19
                D = 1;
            end
            subplot(cntSubplots,(cntMuscles/cntSubplots), (cntMuscles/cntSubplots)*(I-1) + J)
            hold on
            plot(emgf.Time, emgf.Data(:,K), '-')
            plot(refaraw.Time, 1e6*(refaraw.Data(:,73)-254.5),'r-')
            grid on
            hold off
            xlim([0, emgf.Time(end)])
            if min(emgf.Data(:,K))~=0 || max(emgf.Data(:,K))~=0
                ylim([min(emgf.Data(:,K)), max(emgf.Data(:,K))])
            end

            tl = title(sprintf('\\textit{%s}', muscle_name{K})); % set title
            set(tl,'interpreter','latex'); % LaTeX style for titel

            yl = ylabel('EMG[$\mu$V]');
            set(yl,'interpreter','latex'); % LaTeX style for y-label
            if I < cntSubplots
                set(gca,'XTickLabel','')
            else
                xl = xlabel('$t$ [s]');
                set(xl,'interpreter','latex'); % LaTeX style for x-label
            end
            set(gca,'FontSize',font_size)
        end
    end
end
